home *** CD-ROM | disk | FTP | other *** search
- #
- # This file is part of OpenVIP (http://openvip.sourceforge.net)
- #
- # Copyright (C) 2002-2003
- # Michal Dvorak, Jiri Sedlar, Antonin Slavik, Vaclav Slavik, Jozef Smizansky
- #
- # This program is licensed under GNU General Public License version 2;
- # see file COPYING in the top level directory for details.
- #
- # Conversion from model.timeline object format to network. Important function
- # is data_to_network(...) which returns string with xml represantation of
- # network.
- #
-
-
- import xml.dom.minidom
- import model
- import string
- import conv
- import globals
- import copy
- import math
-
- input_class_name = 'Input'
- output_class_name ='Output'
-
- AUDIO_DATA_FORMAT = 1
- VIDEO_DATA_FORMAT = 1
- DEFAULT_FPS = 24
-
-
- #same as xml.dom.minidom.Document only writes also doctype information
- class myDocument(xml.dom.minidom.Document):
- def writexml(self, writer, indent="", addindent="",
- newl="", encoding = None):
- writer.write('<?xml version="1.0" ?>\n')
- if self.doctype is not None:
- writer.write("<!DOCTYPE %s PUBLIC\n\t \"%s\"\n\t\"%s\">"\
- %(self.doctype.name,self.doctype.publicId,self.doctype.systemId))
- for node in self.childNodes:
- node.writexml(writer, indent, addindent, newl)
-
- #object for creating dom elements
- g_dom = None
-
-
- #for generating module's ids
- class incremental:
- def __init__(self):
- self.__dict = {}
-
- def get(self,item):
- if not self.__dict.has_key(item):
- self.__dict[item] = 0
- self.__dict[item] = self.__dict[item] + 1
- return self.__dict[item] - 1
-
-
- inc = None
-
-
- def addConnection(m_in,m_out,conn_name,cin_cnt,cout_cnt,node):
- con = g_dom.createElement("connect")
- con.setAttribute("module_in",m_in)
- con.setAttribute("module_out",m_out)
- con.setAttribute("conn_in","%s%d"%(conn_name,cin_cnt))
- con.setAttribute("conn_out","%s%d"% (conn_name,cout_cnt))
- node.appendChild(con)
-
-
- def storeParams(params,node):
- for key, value in params.items():
- par = g_dom.createElement("param")
- par.setAttribute("name",key)
- par.appendChild(g_dom.createTextNode(value))
- node.appendChild(par)
-
- def addLoader(object,node,opened_f):
- if opened_f.has_key(object.src_spec["filename"]):
- return opened_f[object.src_spec["filename"]]
-
- el = g_dom.createElement("module")
- id = "loader%d" % inc.get("loader")
- el.setAttribute("id",id)
- el.setAttribute("class",input_class_name)
- if checkTrack(object) == "overlay":
- tmp = {}
- tmp["filename"] = object.src_spec["filename"]
- storeParams(tmp,el)
- else: storeParams(object.src_spec,el)
- node.appendChild(el)
- opened_f[object.src_spec["filename"]] = id
- return id
-
-
- def checkTrack(object):
- if object.track[0:2] == 'VA' or object.track[0:2] == 'VB' or object.track[0:2] == 'VFx':
- return "video"
-
- if object.track[0:2] == 'AA' or object.track[0:2] == 'AB' or object.track[0:2] == 'VFx':
- return "audio"
- return "overlay"
-
- def addFilters(object,node,lid):
- last = lid
- type = checkTrack(object)
- for filter in object.filters:
- if not filter.active: continue
- el = g_dom.createElement("module")
- id = "filter%d" % inc.get("filter")
- el.setAttribute("id",id)
- filtername = filter.params
- if object.track[0] == "V":
- el.setAttribute("class","VideoFilter")
- filtername["videofilter"] = filter.name
- else:
- el.setAttribute("class","AudioFilter")
- filtername["audiofilter"] = filter.name
-
- storeParams(filtername,el)
- node.appendChild(el)
- addConnection(last,id,type,0,0,node)
- last = id
-
- return last
-
- def addConcat(tr_name,id_array,node,spec_name):
- if spec_name == "": module_name = "concat"
- else : module_name = spec_name
- id = "%s%d" % (module_name,inc.get(module_name))
- el = g_dom.createElement("module")
- el.setAttribute("id",id)
- el.setAttribute("class","Concat")
- par = g_dom.createElement("param")
- par.setAttribute("name","type")
- if tr_name[0] == 'V':
- par.appendChild(g_dom.createTextNode("video"))
- if tr_name[0] == 'A':
- par.appendChild(g_dom.createTextNode("audio"))
- el.appendChild(par)
-
- par = g_dom.createElement("param")
- par.setAttribute("name","count")
- par.appendChild(g_dom.createTextNode("%d"%len(id_array)))
- el.appendChild(par)
- node.appendChild(el)
- return id
-
- def addConcatenates(track,node,spec_name):
- c_ids = {}
- for (tr_name,id_array) in track.items():
- id = addConcat(tr_name,id_array,node,spec_name)
- c_ids[tr_name] = id
- conn = 0
- if tr_name[0] == 'V' :
- conn_name = "video"
- if tr_name[0] == 'A' :
- conn_name = "audio"
-
-
- if len(id_array) > 0:
- for modul in id_array:
- addConnection(modul[0],id,conn_name,0,conn,node)
- conn = conn + 1
-
- return c_ids
-
- def addSubset(track,node,last_id,time_from,time_to):
- if track[0] == 'V':
- type = "video"
- if track[0] == 'A':
- type = "audio"
-
- id = "subset%d" % inc.get("subset")
- el = g_dom.createElement("module")
- el.setAttribute("id",id)
- el.setAttribute("class","Subset")
- addParam("type",type,el)
- addParam("pos","%10.10f"%time_from,el)
- addParam("length","%10.10f"%(time_to - time_from),el)
- node.appendChild(el)
- addConnection(last_id,id,type,0,0,node)
- return id
-
- def addResize(object,node,last_filter_id,quality,vf):
- id = "resize%d" % inc.get("resize")
- el = g_dom.createElement("module")
- el.setAttribute("id",id)
- el.setAttribute("class","VideoFilter")
- if quality == "preview":
- addParam("videofilter","SimpleResize",el)
- if quality == "final":
- addParam("videofilter","Resize",el)
- addParam("width","%d"%vf.width,el)
- addParam("height","%d"%vf.height,el)
-
- node.appendChild(el)
- if object.track[0] == "V":type = "video"
- else: type = "audio"
- addConnection(last_filter_id,id,type,0,0,node)
- return id
-
-
-
- def addFPSChanger(object,node,last_filter_id,vf,start,end):
- id = "fps_changer%d" % inc.get("fps_changer")
- el = g_dom.createElement("module")
- el.setAttribute("id",id)
- el.setAttribute("class","FPSChanger")
- o_len = min(object.time_to,end) - max(object.time_from,start)
- addParam("fps","%3.10f"%vf.fps,el)
- node.appendChild(el)
- if object.track[0] == "V":type = "video"
- else: type = "audio"
- addConnection(last_filter_id,id,type,0,0,node)
- return id
-
-
- def c_f(i1,i2):
- return i1[1] - i2[1]
-
- def addParam(name,value,node):
- par = g_dom.createElement("param")
- par.setAttribute("name",name)
- par.appendChild(g_dom.createTextNode(value))
- node.appendChild(par)
-
- def c_f2(i1,i2):
- if i1[1] == i2[1]: return 0
- if i1[1] > i2[1]: return 1
- return -1
-
-
- def addOverlays2(obj_on_track,node,fin,def_video,def_audio,quality,tmln_length,\
- tmln_start,opened_f,obj_fps):
-
- last_audio = fin["AA"]
- last_video = fin["VA"]
- for key, value in obj_on_track.items():
- if key not in ["VA","VB","AA","AB","VFx","AFx"]:
- id = "overlay%d" % inc.get("overlay")
-
- if key[0]=="V":
- if def_video == None:continue
- type = "video"
- if last_video == None:
- last_video = addNullGen("V",def_video,def_audio,node,\
- tmln_length)
- last = last_video
- last_video = id
- if key[0]=="A":
- if def_audio == None:continue
- type = "audio"
- if last_audio == None:
- last_audio = addNullGen("A",def_video,def_audio,node,\
- tmln_length)
- last = last_audio
- last_audio = id
- new_input_id = storeTrack(value,key,node,\
- def_video,def_audio,quality,tmln_length,\
- tmln_start,opened_f,obj_fps,last)
-
- el = g_dom.createElement("module")
- el.setAttribute("id",id)
- el.setAttribute("class","Overlay")
- tmp = {}
- if value[0].src_spec.has_key("static_overlay") and \
- value[0].src_spec["static_overlay"] == "true":
- tmp["static_overlay"]="1"
- if value[0].src_spec.has_key("x"):
- tmp["x"]=value[0].src_spec["x"]
- if value[0].src_spec.has_key("y"):
- tmp["y"]=value[0].src_spec["y"]
-
- storeParams(tmp,el)
- node.appendChild(el)
- addConnection(last ,id,type,0,0,node)
- addConnection(new_input_id,id,type,0,1,node)
-
- fin["AA"] = last_audio
- fin["VA"] = last_video
- return fin
-
-
- def addSaver(node,fin,out_params):
- if fin["AA"] == None and fin["VA"] == None: return None
-
- el = g_dom.createElement("module")
- if out_params.has_key("filename") and out_params["filename"] != None:
- id = "saver%d" % inc.get("saver")
- el.setAttribute("class",output_class_name)
- storeParams(out_params,el)
- else:
- # Store into memory;
- id = "saver"
- el.setAttribute("class", "openvip.highlevel_api.MemOutput")
- el.setAttribute("id",id)
-
- node.appendChild(el)
- if fin["AA"] != None:addConnection(fin["AA"],id,"audio",0,0,node)
- if fin["VA"] != None:addConnection(fin["VA"],id,"video",0,0,node)
- return id
-
- def objcmp(o1,o2):
- if o1.time_from < o2.time_from: return -1
- if o1.time_from == o2.time_from: return 0
- return 1
-
- def redist(data,ot,tt):
- for obj in data.objects:
- if not ot.has_key(obj.track):
- ot[obj.track] = []
- ot[obj.track].append(obj)
- for tr in ot.values(): tr.sort(objcmp)
- for trans in data.transitions:
- if not tt.has_key(trans.track):
- tt[trans.track] = []
- tt[trans.track].append(trans)
- for tr in tt.values(): tr.sort(objcmp)
-
-
-
- def addNullGen(type,def_video,def_audio,node,length):
- id = "nullgen%d" % inc.get("nullgen")
- el = g_dom.createElement("module")
- el.setAttribute("id",id)
- el.setAttribute("class","NullGenerator")
- if type[0] == 'V':
- addParam("type","video",el)
- addParam("fps","%f"%def_video.fps,el)
- addParam("frame_width","%d"%def_video.width,el)
- addParam("frame_height","%d"%def_video.height,el)
- addParam("frame_format","%d"%VIDEO_DATA_FORMAT,el)
- addParam("aspect_ratio","%f"%def_video.aspect_ratio,el)
-
- if type[0] == 'A':
- addParam("type","audio",el)
- addParam("samplerate","%d"%def_audio.sample_rate,el)
- addParam("channels","%d"%def_audio.channels,el)
- addParam("audio_format","%d"%AUDIO_DATA_FORMAT,el)
- addParam("length","%f"%length,el)
-
- node.appendChild(el)
- return id
-
- def storeTrack(objects,type,node,def_video,def_audio,quality,tmln_length,tmln_start,opened_f,\
- obj_fps,input_id_for_overlays):
- last_time = tmln_start
- ids = []
- for object in objects:
- #if object is completly out of computed interval
- if object.time_to < tmln_start or object.time_from > tmln_start+tmln_length:
- continue
- #if there is a space between objects
- if object.time_from > last_time:
- if input_id_for_overlays == None:
- ng_id = addNullGen(type,def_video,def_audio,node,\
- object.time_from - last_time)
- else:
- ng_id = addSubset(object.track,node,input_id_for_overlays,\
- last_time,object.time_from)
- ids.append(ng_id)
-
- last_id = addLoader(object,node,opened_f)
-
- #if object is not whole in interval
- #sub_from,sub_to parameters for Subset
- sub_from = object.src_from
- if object.src_to != 0:sub_to = object.src_to
- else: sub_to = sub_from + object.time_to - object.time_from
-
- tmln_to = tmln_start + tmln_length
-
- if tmln_start > object.time_from:
- sub_from = sub_from + tmln_start - object.time_from
-
- if tmln_to < object.time_to:
- sub_to = sub_from + tmln_to - max(object.time_from,tmln_start)
-
- if object.src_from != 0 or\
- object.src_to != 0 or\
- sub_to - sub_from != object.time_to - object.time_from:
- last_id = addSubset(object.track,node,last_id,sub_from,sub_to)
-
-
- last_id = addFilters(object,node,last_id)
- #video must be resized and reFPSed
- if object.track[0] == "V":
- if not obj_fps.has_key(object.id) or\
- obj_fps[object.id].height != def_video.height or\
- obj_fps[object.id].width != def_video.width:
- last_id = addResize(object,node,last_id,quality,def_video)
- if not obj_fps.has_key(object.id) or\
- obj_fps[object.id].fps != def_video.fps:
- last_id = addFPSChanger(object,node,last_id,def_video,\
- tmln_start,tmln_start+tmln_length)
-
-
-
- ids.append(last_id)
- last_time = min(tmln_start + tmln_length,object.time_to)
-
- #if last object doesn't end at the end of the interval
- if (last_time < tmln_start + tmln_length) and (len(ids)>0):
- if input_id_for_overlays == None:
- ng_id = addNullGen(type,def_video,def_audio,node,\
- tmln_start + tmln_length - last_time)
- else:
- ng_id = addSubset(object.track,node,input_id_for_overlays,\
- last_time,tmln_start + tmln_length)
- ids.append(ng_id)
-
- if len(ids)>1:
- conid = addConcat(type,ids,node,"")
- if type[0] == "V":conn_name = "video"
- else: conn_name = "audio"
- n = 0
-
- for id in ids:
- addConnection(id,conid,conn_name,0,n,node)
- n =n + 1
- else:
- if len(ids)==1: conid = ids[0]
- else:conid = None
- return conid
-
- def addTrans(trans,node,def_video,def_audio,length):
- id = "trans%d" % inc.get("trans")
- el = g_dom.createElement("module")
- el.setAttribute("id",id)
- add_params = trans.params
- if trans.track[0] == "V":
- el.setAttribute("class","VideoTransition")
- add_params["transition"] = trans.name
- add_params["length"] = str(int(math.ceil(length*def_video.fps)))
- else:
- el.setAttribute("class","AudioTransition")
- add_params["transition"] = trans.name
- add_params["length"] = str(int(math.ceil(length*def_audio.sample_rate)))
-
- storeParams(trans.params,el)
- node.appendChild(el)
- return id
-
- #Removes objects which are not in (start,end) interval
- #objects which are partialy in the interval are cuted to current length
- def cutObjects(objects,start,end):
- to_delete = []
- for o in objects:
- if o.time_to <= start or o.time_from >= end:
- to_delete.append(o)
- continue
- if o.time_from < start:
- o.time_from = start
- if o.time_to > end:
- o.time_to = end
- for o in to_delete:
- objects.remove(o)
-
- def oot_sort(o1,o2):
- if o1.start < o2.start: return -1
- if o1.start == o2.start: return 0
- return 1
-
- def addObjectsToAllTracks(all_tracks,objects,id):
- to_add = []
- to_delete = []
- for o in objects:
- o_id = id
- if id == "trans": o_id = o.id
- oot = objOnTrack(o.time_from,o.time_to,o_id)
- for o2 in all_tracks:
- if o2.start < oot.start and o2.end > oot.start and o2.end <= oot.end:
- o2.end = oot.start
- if o2.start >= oot.start and o2.start < oot.end and o2.end > oot.end:
- o2.start = oot.end
- if o2.start < oot.start and o2.end > oot.end:
- new_obj = objOnTrack(o2.start,o2.end,o2.id)
- o2.end = oot.start
- new_obj.start = oot.end
- all_tracks.append(new_obj)
- if o2.start >= oot.start and o2.end <= oot.end:
- to_delete.append(o2)
- to_add.append(oot)
- for o in to_delete:
- all_tracks.remove(o)
- all_tracks.extend(to_add)
- all_tracks.sort(oot_sort)
-
-
- class objOnTrack:
- def __init__(self,s,e,id):
- self.start = s
- self.end = e
- self.id = id
-
-
- def fillSpaces(arr,id,start,end):
- last = start
- to_add = []
- for o in arr + [objOnTrack(end,end,None)]:
- if o.start > last:
- o2 = objOnTrack(last,o.start,id)
- to_add.append(o2)
- last = o.end
- arr.extend(to_add)
- arr.sort(oot_sort)
-
- def chainSame(arr):
- while 1:
- to_del = []
- for i in range(len(arr)-1):
- if arr[i].id == arr[i+1].id:
- to_del.append(arr[i])
- arr[i+1].start = arr[i].start
- if len(to_del) == 0:break
- for o in to_del:arr.remove(o)
-
- def addTransitions(AVtype,arr,transitions,node,A_id,B_id,def_video,def_audio,start,end):
- if AVtype[0] == "V":type = "video"
- else: type = "audio"
-
- for o in arr:
- if o.id != A_id and o.id != B_id:
- direction = 0
- for tr in transitions:
- if tr.id == o.id:
- if hasattr(tr,"direction") and tr.direction=="BA":
- direction = 1
- o.id = addTrans(tr,node,def_video,def_audio,\
- o.end-o.start)
- break
- if tr.time_from != start or tr.time_to != end:
- s_id = addSubset(tr.track,node,A_id,\
- tr.time_from - start,tr.time_to - start)
- else:s_id = A_id
- addConnection(s_id,o.id,type,0,direction,node)
- if tr.time_from != start or tr.time_to != end:
- s_id = addSubset(tr.track,node,B_id,\
- tr.time_from - start,tr.time_to - start)
- else:s_id = B_id
- addConnection(s_id,o.id,type,0,1-direction,node)
-
-
- def mixTracks2(AVtype,objectsA,objectsB,trans,A_id,B_id,node,\
- def_video,def_audio,tmln_length,tmln_start):
-
- cutObjects(objectsA,tmln_start,tmln_start+tmln_length)
- cutObjects(objectsB,tmln_start,tmln_start+tmln_length)
- cutObjects(trans,tmln_start,tmln_start+tmln_length)
-
- all_tracks = []
- addObjectsToAllTracks(all_tracks,objectsA,A_id)
- addObjectsToAllTracks(all_tracks,objectsB,B_id)
- addObjectsToAllTracks(all_tracks,trans,"trans")
-
- if B_id: fillSpaces(all_tracks,B_id,tmln_start,tmln_start+tmln_length)
- else: fillSpaces(all_tracks,A_id,tmln_start,tmln_start+tmln_length)
-
- chainSame(all_tracks)
-
- addTransitions(AVtype,all_tracks,trans,node,A_id,B_id,def_video,def_audio,\
- tmln_start,tmln_start+tmln_length)
-
- if len(all_tracks) > 1:
- conid = addConcat(AVtype,all_tracks,node,"")
- n = 0
- if AVtype[0] == "V":type = "video"
- else: type = "audio"
- for o in all_tracks:
- if o.id == A_id or o.id == B_id:
- o.id = addSubset(AVtype,node,o.id,o.start - tmln_start,\
- o.end - tmln_start)
- addConnection(o.id,conid,type,0,n,node)
- n =n + 1
- else:
- if len(all_tracks) == 1:
- conid = all_tracks[0].id
- else: conid = None
-
- return conid
-
-
-
- def selectImplicitFPS(v_obj,obj_fps):
- all = []
- for o in v_obj:
- i = globals.get_file_info(o.src_spec["filename"])
- for v in i.video_streams:
- all.append(v.fps)
- obj_fps[o.id] = copy.copy(v)
- if len(all) == 0: return DEFAULT_FPS
- all.sort()
- all.reverse()
- val = all[0]
- ret = val
- max_len = 0
- cur_len = 0
- for cur in all:
- if cur == val:cur_len = cur_len + 1
- else:
- if cur_len > max_len:
- max_len = cur_len
- ret = val
- val = cur
- cur_len = 0
- if cur_len > max_len:
- max_len = cur_len
- ret = val
- return ret
-
- def getSpaces(tr,obj,start,end):
- ret = []
- last = start
- if obj.has_key(tr):
- for o in obj[tr]:
- if o.time_from > last:
- ret.append((last,o.time_from))
- last = o.time_to
- if last < end:ret.append((last,end))
- else:ret.append((start,end))
- return ret
-
- def cutTrans(trans,spaces):
- to_add = []
- to_del = []
- for tr in trans:
- for s in spaces:
- if s[0] <= tr.time_from and s[1] > tr.time_from and s[1] < tr.time_to:
- tr.time_from = s[1]
- if s[0] < tr.time_to and s[1] >= tr.time_to and s[0] > tr.time_from:
- tr.time_to = s[0]
- if s[0] <= tr.time_from and s[1] >= tr.time_to:
- to_del.append(tr)
- if s[0] > tr.time_from and s[1] < tr.time_to:
- tr2 = copy.deepcopy(tr)
- tr2.id = tr.id + "string@that$_none%could&use-as|name for transition()in_his timeline:-){I hope}+=wdkljvw"
- tr.time_to = s[0]
- tr2.time_from = s[1]
- to_add.append(tr2)
-
- for tr in to_del:
- trans.remove(tr)
- trans.extend(to_add)
-
-
- def recountTransitions(obj,trans,start,end):
- for tr in ["VFx","AFx"]:
- if trans.has_key(tr):
- spaces = getSpaces(tr[0]+"A",obj,start,end)
- cutTrans(trans[tr],spaces)
- spaces = getSpaces(tr[0]+"B",obj,start,end)
- cutTrans(trans[tr],spaces)
-
-
- def extractObjects(data,node,def_video,def_audio,quality,output_params,\
- tmln_length,tmln_start):
- """Extracts objects, mixs them through transitions, adds overlays and Saver modul"""
-
- obj_on_track = {}
- trans = {}
- redist(data,obj_on_track,trans)
- recountTransitions(obj_on_track,trans,tmln_start,tmln_start+tmln_length)
-
- make_video = make_audio = 1
- if def_audio == None:make_audio = 0
- if def_video == None:make_video = 0
- opened_f = {}
-
- obj_fps = {}
- if make_video:
- video_for_fps = []
- if obj_on_track.has_key("VA"):video_for_fps.extend(obj_on_track["VA"])
- if obj_on_track.has_key("VB"):video_for_fps.extend(obj_on_track["VB"])
- impl_fps = selectImplicitFPS(video_for_fps,obj_fps)
- if def_video.fps == None:def_video.fps = impl_fps
- if obj_on_track.has_key("VA"):
- VA_id = storeTrack(obj_on_track["VA"],"VA",node,\
- def_video,def_audio,quality,tmln_length,\
- tmln_start,opened_f,obj_fps,None)
- else: VA_id = None
- if obj_on_track.has_key("VB"):
- VB_id = storeTrack(obj_on_track["VB"],"VB",node,\
- def_video,def_audio,quality,tmln_length,\
- tmln_start,opened_f,obj_fps,None)
- else: VB_id = None
- if not trans.has_key("VFx"):trans["VFx"] = []
- if not obj_on_track.has_key("VA"):obj_on_track["VA"] = []
- if not obj_on_track.has_key("VB"):obj_on_track["VB"] = []
- video_id =mixTracks2("V",obj_on_track["VA"],obj_on_track["VB"],\
- trans["VFx"],VA_id,VB_id,node,\
- def_video,def_audio,tmln_length,tmln_start)
- else: video_id = None
-
- if make_audio:
- if obj_on_track.has_key("AA"):
- AA_id = storeTrack(obj_on_track["AA"],"AA",node,def_video,\
- def_audio,quality,tmln_length,tmln_start,opened_f,None,None)
- else: AA_id = None
- if obj_on_track.has_key("AB"):
- AB_id = storeTrack(obj_on_track["AB"],"AB",node,def_video,def_audio,\
- quality,tmln_length,tmln_start,opened_f,None,None)
- else: AB_id = None
- if not trans.has_key("AFx"):trans["AFx"] = []
- if not obj_on_track.has_key("AA"):obj_on_track["AA"] = []
- if not obj_on_track.has_key("AB"):obj_on_track["AB"] = []
- audio_id =mixTracks2("A",obj_on_track["AA"],obj_on_track["AB"],\
- trans["AFx"],AA_id,AB_id,node,\
- def_video,def_audio,tmln_length,tmln_start)
- else: audio_id = None
-
-
- fin = {}
- fin["AA"] = audio_id
- fin["VA"] = video_id
-
- fin = addOverlays2(obj_on_track,node,fin,def_video,def_audio,quality,tmln_length,\
- tmln_start,opened_f,obj_fps)
-
- addSaver(node,fin,output_params)
-
- def v_to_dict(v):
- d = {}
- d["width"] = v.width
- d["height"] = v.height
- d["fps"] = v.fps
- d["aspect"] = v.aspect
- return d
- def a_to_dict(a):
- d = {}
- d["sample_rate"] = a.sample_rate
- d["channels"] = a.channels
- return d
-
- def getHeighestMostFrequentValue(d,key):
- a = {}
-
- for o in d:
- if not a.has_key(o[key]):a[o[key]] = 0
- a[o[key]] = a[o[key]] + 1
-
- max_val = 0
- max_key = 0
-
- for key,val in a.items():
- if max_val == val and max_key < int(key):
- max_key = key
- if max_val < val:
- max_val = val
- max_key = key
- return max_key
-
-
- def getDefaultFormat(type,data,tmln_length,tmln_start):
- cutObjects(data.objects,tmln_start,tmln_start+tmln_length)
-
- all_video = []
- all_audio = []
- for o in data.objects:
- i = globals.get_file_info(o.src_spec["filename"])
- if type == "video" and o.track[0] == "V":
- for v in i.video_streams:
- all_video.append(v_to_dict(v))
- if type == "audio" and o.track[0] == "A":
- for a in i.audio_streams:
- all_audio.append(a_to_dict(a))
-
- ret = None
- if type == "video" and len(all_video) > 0:
- width = getHeighestMostFrequentValue(all_video,"width")
- height= getHeighestMostFrequentValue(all_video,"height")
- fps = getHeighestMostFrequentValue(all_video,"fps")
- aspect= getHeighestMostFrequentValue(all_video,"aspect")
- ret = model.VideoFormat(width,height,fps,aspect)
- if type == "audio" and len(all_audio) > 0:
- sample_rate = getHeighestMostFrequentValue(all_audio,"sample_rate")
- channels = getHeighestMostFrequentValue(all_audio,"channels")
- ret = model.AudioFormat(sample_rate,channels)
- return ret
-
-
- def getBestAudioFormat(data,tmln_length,tmln_start):
- """Returns best output parameters for audio objects in the part of timeline
- determined by tmln_length,tmln_start"""
- return getDefaultFormat("audio",data,tmln_length,tmln_start)
-
- def getBestVideoFormat(data,tmln_length,tmln_start):
- """Returns best output parameters for video objects in the part of timeline
- determined by tmln_length,tmln_start"""
- return getDefaultFormat("video",data,tmln_length,tmln_start)
-
- #Main function. Returns string with xml network representation
- #data - input model object; xmlns,version - xml specifications
- #def_video/audio - (video|audio)Format object with default values
- #def_video/audio == 0 means that this video/audio part will be not counted
- #quality - preview/final information for Resize module
- #f_out - filename for output; tmln_length,tmln_start - timline parameters
- def data_to_network(data,xmlns,version,def_video,def_audio,quality,\
- output_params,tmln_length,tmln_start = 0):
- """Converts model object into xml representation of network (set of
- modules and connections) readable for OpenVIP core. def_video
- def_audio are objects(class videoFormat/audioFormat) with default
- values for video/audio track if some them is None it means, that
- relevant (vide/audio) tracks will be not computed. quality(preview/final)
- parameter defines what kind of risize module will be used for video objects.
- In output_params are values for output modul. tmln_length,tmln_start determines
- part of timeilne which will be computed
- Returns string with xml data."""
-
- global g_dom, inc
- g_dom = myDocument()
- inc = incremental()
-
-
- doctype = xml.dom.minidom.DocumentType("network")
- doctype.publicId = "-//OPENVIP//DTD Network Format V1.0//EN"
- doctype.systemId = "http://openvip.sourceforge.net/dtds/openvip-network.dtd"
- g_dom.doctype = doctype
-
- el = g_dom.createElement("network")
- el.setAttribute("version",version)
- el.setAttribute("xmlns",xmlns)
- import copy
- data2 = copy.deepcopy(data)
- conv.checkInput(data2)
- extractObjects(data2,el,def_video,def_audio,quality,output_params,tmln_length,tmln_start)
- g_dom.appendChild(el)
-
- # s = g_dom.toprettyxml(indent=' ')
- s = g_dom.toxml()
-
- g_dom.unlink()
-
- return s
-
-